package com.lfg.leetcode.leetcode100;


import java.util.Stack;

public class No150evalRPN {

    public static void main(String[] args) {
        int ret = evalRPN(new String[]{"4", "13", "5", "/", "+"});
        System.out.println(ret);
    }

    public static int evalRPN(String[] tokens) {
        int ret = 0;
        if (null == tokens || tokens.length == 0) {
            return ret;
        }
        Stack<Integer> stack = new Stack<Integer>();
        for (String str : tokens) {
            if ("+".equals(str)) {
                int a = stack.pop();
                int b = stack.pop();
                ret = a + b;
                stack.push(ret);
            } else if ("-".equals(str)) {
                int a = stack.pop();
                int b = stack.pop();
                ret = b - a;
                stack.push(ret);
            } else if ("*".equals(str)) {
                int a = stack.pop();
                int b = stack.pop();
                ret = b * a;
                stack.push(ret);
            } else if ("/".equals(str)) {
                int a = stack.pop();
                int b = stack.pop();
                ret = b / a;
                stack.push(ret);
            } else {
                stack.push(Integer.valueOf(str));
            }
        }
        return stack.pop();
    }
}
